package com.homegoods.login.ui;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.ArrayMap;
import android.widget.ArrayAdapter;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.login.R;
import com.homegoods.login.databinding.LoginActivityBindingImpl;
import com.homegoods.login.evnet.LoginSucEvent;
import com.homegoods.login.model.UserProxy;
import com.homegoods.login.presenter.LoginPresenter;
import com.homegoods.login.view.LoginView;
import com.homegoods.aroutercomment.ARouterPath;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.util.KeyBoardUtils;
import com.kedll.library.util.MyLog;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.RxBus;
import com.kedll.library.util.StringUtil;
import com.kedll.library.util.ToastUtil;
import com.kedll.library.widget.AIAutoCompleteTextView;
import com.share.sharelibrary.ThirdLoginView;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;
import java.util.Objects;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * @author QiQi
 * @ClassName: LoginActivity
 * @date 2018/9/12
 * @description 登录注册
 *
 * shibo.zheng
 * ***************废弃****************
 */
@RuntimePermissions
@Route(path = ARouterPath.PATH_LOGIN)
public class LoginActivity extends MyBaseFragmentActivity<LoginActivityBindingImpl, LoginPresenter> implements LoginView {

    private AIAutoCompleteTextView mPhone;
    private ThirdLoginView thirdLoginView;

    @Override
    protected int getLayout() {
        return R.layout.login_activity;
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initEvent() {

        LoginActivityPermissionsDispatcher.requestUMWithPermissionCheck(this);
        RxBus.getDefault().toFlowable(LoginSucEvent.class)
                .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(loginSucEvent -> {
                    ToastUtil.showToast(R.string.login_suc);
                    ARouter.getInstance().build(ARouterPath.PATH_MAIN_HOME).navigation();
                });
        RxTextView.afterTextChangeEvents(mBinding.etPwd)
                .compose(bindToLifecycle())
                .subscribe(textChangeEvent -> mBinding.btnLogin.setEnabled(
                        Objects.requireNonNull(textChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mPhone.getText())));

        RxTextView.afterTextChangeEvents(mBinding.phone)
                .compose(bindToLifecycle())
                .subscribe(textChangeEvent -> mBinding.btnLogin.setEnabled(
                        Objects.requireNonNull(textChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mBinding.etPwd.getText())));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        LoginActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @Override
    public void showLoad(String msg) {
        MyLog.getInstance().showProgressDialog(this, msg);
    }

    @Override
    public void hideLoad() {
        MyLog.getInstance().dismissDialog();
    }

    @Override
    public Context getContext() {
        return getApplicationContext();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        UserProxy.getInstance().init(getApplicationContext());
//        mPresenter.initAutoComplete(this);//获取登录过的历史账号
        mBinding.setLogin(mPresenter);
        mPhone = mBinding.phone;

        //文字下划线
        mBinding.userAgreement.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        mBinding.userAgreement.getPaint().setAntiAlias(true);


        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white);

        //第三方登录
        thirdLoginView = mBinding.thirdLogin;

        thirdLoginView.setColumnNum(2);
        thirdLoginView.hideHeadView();

        Map<SHARE_MEDIA, Integer> map = new ArrayMap<>();
        map.put(SHARE_MEDIA.WEIXIN, R.mipmap.wechat_login);
        map.put(SHARE_MEDIA.QQ, R.mipmap.qq_login);

        thirdLoginView.setPlatForm(map, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QQ);

        thirdLoginView.setLogin(access_token ->
                mPresenter.qqLogin(access_token));
    }

    @Override
    protected LoginPresenter getPresenter() {
        return/* new LoginPresenter(this)*/null;
    }

    @Override
    protected void onStart() {
        super.onStart();
        MyUtils.setSystemUi(getWindow(), false);
    }

    @Override
    protected void onStop() {
        super.onStop();
        MyUtils.setSystemUi(getWindow(), true);
    }

    /**
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (resultCode == RESULT_CANCELED || data == null) {
                return;
            }

            String phoneNum = data.getStringExtra("phoneNum");
            mPhone.setText(phoneNum);
            mPhone.setSelection(phoneNum.length());

        } else {
            thirdLoginView.onResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void hideKeyBoard() {
        KeyBoardUtils.hideKeyBoard(this);
    }

    @Override
    public void showRegisterView() {
        ARouter.getInstance()
                .build(ARouterPath.PATH_REGISTER)
                .navigation(this, 100);
    }

    @Override
    public void showRestPwdView() {
        ARouter.getInstance()
                .build(ARouterPath.PATH_REST_PWD)
                .navigation();
    }

    @Override
    public void setAutoTextViewAdapter(ArrayAdapter adapter) {
        mPhone.setAdapter(adapter);
    }

    @NeedsPermission({Manifest.permission.READ_CALENDAR, Manifest.permission.GET_ACCOUNTS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void requestUM() {
    }
}
